<?php

namespace App\Admin\Controllers;

use App\Admin\Grid\NGrid;
use App\Models\ShopProductSpecsModel;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Grid\Filter;

class ShopProductSpecController extends AdminController
{
    protected $title = '规格1';

    /**
     * grid
     * @return NGrid
     */
    protected function grid(): NGrid
    {
        $grid = new NGrid(new ShopProductSpecsModel);
        $grid->model()->orderByDesc('created_at');
        $grid->filter(function (Filter $filter) {
            $filter->disableIdFilter();
            $filter->column(1 / 2, function (Filter $filter) {
                $filter->equal('ps_id', 'ID')->integer();
                $filter->equal('product_id', '商品ID')->integer();
            });
            $filter->column(1 / 2, function (Filter $filter) {
                $filter->like('serial_number', __('l.serialNumber'));
            });
        });
        $grid->column('ps_id', 'ID')->sortable();
        $grid->column('serial_number', __('l.serialNumber'));
        $grid->column('ps_avatar', '图片')->display(function ($column) {
            if (!is_null($column)) return view('image', ['id' => $this->ps_id, 'image' => fileIdToUrl($column)]);
            else return null;
        });
        $grid->column('product_id', '商品ID');
        $grid->column('rand_product_id', '随机ID');
        $grid->column('user_id', '用户ID');
        $grid->column('ps_sort', '排序');
        $grid->column('ps_name', '名称');
        $grid->column('ps_price', '优惠价');
        $grid->column('ps_price_underlined', '原价');
        $grid->column('ps_total_stock', '总库存');
        $grid->column('ps_usable_stock', '可用库存');
        $grid->column('ps_disable_stock', '不可用库存');
        $grid->column('ps_is_delete', '是否删除')->using([1 => '是', 2 => '否']);
        $grid->column('ps_buy_limit', '限购');
        $grid->column('commission', '佣金');
        $grid->column('commission_ratio', '佣金比例');
        $grid->column('sales', '销量');
        $grid->column('created_at', __('l.createdAt'))->display(function ($column) {
            return now()->timestamp($column)->toDateTimeString();
        });
        return $grid;
    }
}
